package com.gmall.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Sku_Queue_Config {


    public static final String EXCHANGE_DIRECT_GOODS = "exchange.direct.goods";
    public static final String ROUTING_GOODS_UPPER = "goods.upper";
    public static final String ROUTING_GOODS_LOWER = "goods.lower";
    //队列
    public static final String QUEUE_GOODS_UPPER  = "queue.goods.upper";
    public static final String QUEUE_GOODS_LOWER  = "queue.goods.lower";


    // 声明交换机
    @Bean(EXCHANGE_DIRECT_GOODS)
    public Exchange EXCHANGE_DIRECT_GOODS(){
        //Builder 设计模式
        return ExchangeBuilder.directExchange(EXCHANGE_DIRECT_GOODS)
                .durable(true)//默认RabbitMQ 重启以后， 交换机就删掉，除非是durable=true
                .build();
    }

    // 声明队列
    @Bean(QUEUE_GOODS_UPPER)
    public Queue QUEUE_GOODS_UPPER(){
        return QueueBuilder.durable(QUEUE_GOODS_UPPER).build();
    }

    @Bean(QUEUE_GOODS_LOWER)
    public Queue QUEUE_GOODS_LOWER(){
        return QueueBuilder.durable(QUEUE_GOODS_LOWER).build();
    }

    @Bean("directBindingUpper")
    public Binding directBindingUpper(@Qualifier(QUEUE_GOODS_UPPER) Queue queue,
                                    @Qualifier(EXCHANGE_DIRECT_GOODS) Exchange exchange){

        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_GOODS_UPPER).noargs();
    }

    @Bean("directBindingLower")
    public Binding directBindingLower(@Qualifier(QUEUE_GOODS_LOWER) Queue queue,
                                    @Qualifier(EXCHANGE_DIRECT_GOODS) Exchange exchange){

        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_GOODS_LOWER).noargs();
    }


}
